package com.service.sys;

import com.common.ErrorCodeEnum;
import com.common.emums.DataStatus;
import com.common.exception.BusinessException;
import com.common.utils.StringUtils;
import com.core.springSecurity.common.SecurityUtils;
import com.dao.primary.sys.SysRoleMapper;
import com.github.pagehelper.PageHelper;
import com.model.domain.sys.GetRoleListDO;
import com.model.pojo.sys.SysRole;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author: zql
 * @Description: 角色服务
 * @Data: 17:01 2021/7/28
 */
@Service
public class SysRoleService {

    private static final String PERMISSION_DELIMETER = ",";

    @Resource
    SysRoleMapper sysRoleMapper;

    public static String getKey(Set<String> keys) {
        StringBuilder stringBuffer = new StringBuilder();
        for (String key : keys) {
            stringBuffer.append(key).append(PERMISSION_DELIMETER);
        }
        return stringBuffer.toString();
    }

    public static Set<String> getKeySet(String key) {
        if(StringUtils.isEmpty(key)){
            return new HashSet<>();
        }
        return new HashSet<>(Arrays.asList(key.trim().split(PERMISSION_DELIMETER)));
    }

    public static Set<String> getApiKeySet(Set<SysRole> sysRoleSet) {
        Set<String> permsSet = new HashSet<>();
        sysRoleSet.forEach(sysRole -> permsSet.addAll(SysRoleService.getKeySet(sysRole.getApiKey())));
        return permsSet;
    }

    public static Set<String> getButtonKeySet(Set<SysRole> sysRoleSet) {
        Set<String> buttonSet = new HashSet<>();
        sysRoleSet.forEach(sysRole -> buttonSet.addAll(SysRoleService.getKeySet(sysRole.getButtonKey())));
        return buttonSet;
    }

    /**
     * @Author: zql
     * @Description: 创建角色
     * @param sysRole 角色
     * @Data: 11:15 2021/7/5
     */
    public void create(SysRole sysRole){
        String name = SecurityUtils.getUsername();
        sysRole.setUpdateTime(null);
        sysRole.setCreateTime(null);
        sysRole.setCreator(name);
        sysRole.setModifier(name);
        sysRoleMapper.insertSelective(sysRole);
    }

    /**
     * @Author: zql
     * @Description: 获取角色信息
     * @param sysRoleId 角色id
     * @Data: 11:16 2021/7/5
     */
    public SysRole get(Long sysRoleId,Byte isDelete){
        SysRole sysRole = sysRoleMapper.selectByPrimaryKey(sysRoleId,isDelete);
        if (StringUtils.isNull(sysRole)) {
            throw new BusinessException(ErrorCodeEnum.ROLE_NOT_FOUNT);
        }
        return sysRole;
    }

    /**
     * 获取角色列表
     *
     * @param getRoleListDO 角色
     */
    public List<SysRole> getRoleList(GetRoleListDO getRoleListDO) {
        PageHelper.clearPage();
        PageHelper.startPage(getRoleListDO.getPage(), getRoleListDO.getPageSize(),getRoleListDO.getSortRow()+getRoleListDO.getSortOrder());
        return sysRoleMapper.selectList(getRoleListDO);
    }

    /**
     * @Author: zql
     * @Description: 批量更新角色信息
     * @param sysRoleList 角色列表
     * @Data: 11:12 2021/7/5
     */
    public void updateList(List<SysRole> sysRoleList) {
        String name=SecurityUtils.getUsername();
        sysRoleList.forEach(o->{
            o.setCreator(name);
            o.setModifier(name);
            o.setUpdateTime(null);
            o.setCreateTime(null);
        });
        sysRoleMapper.updateByPrimaryKeySelectiveList(sysRoleList);
    }

    public void update(SysRole sysRole){
        String name=SecurityUtils.getUsername();
        sysRole.setCreator(name);
        sysRole.setModifier(name);
        sysRole.setUpdateTime(null);
        sysRole.setCreateTime(null);
        sysRoleMapper.updateByPrimaryKeySelective(sysRole);
    }

    public boolean has(Long sysRoleId){
       return sysRoleMapper.has(sysRoleId, DataStatus.OK.getCode())>0;
    }
}
